<template>
  <div>
    <el-dialog
      :title="info.isadd?'添加':'修改'"
      :visible.sync="info.isshow"
      @opened="opened"
      @closed="closed"
    >
      {{form}}
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker
            v-model="form.begintime"
            type="date"
            placeholder="开始时间"
            format="yyyy 年 MM 月 dd 日"
            value-format="timestamp"
          ></el-date-picker>
          <el-date-picker
            v-model="form.endtime"
            type="date"
            placeholder="结束时间"
            format="yyyy 年 MM 月 dd 日"
            value-format="timestamp"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类">
          <el-select v-model="form.first_cateid" placeholder="请选择" @change="changeonecate">
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in catelist"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择" @change="changetwocate">
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in twocate"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品">
          <el-select v-model="form.goodsid" placeholder="请选择">
            <el-option
              :label="item.goodsname"
              :value="item.id"
              v-for="item in foods"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">{{info.isadd?'添加':'修改'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqseckadd,
  reqseckdit,
  reqcatelist,
  reqgoodslist
} from "../../../request/api.js";
export default {
  components: {},
  data() {
    return {
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1
      },
      twocate: [],
      // attrs: [],
      foods: []
    };
  },
  computed: {
    ...mapGetters({
      info: "seckill/info",
      seckinfo: "seckill/seckinfo",
      catelist: "cate/catelist",
      specslist: "specs/specslist"
    })
  },
  methods: {
    ...mapActions({
      reqsecklist: "seckill/secklist",
      reqcatelist: "cate/catelist",
      reqspecslist: "specs/specslist",
      reqseckadd: "seckill/seckadd"
    }),
    cancel() {
      this.info.isshow = false;
      if (!this.info.isadd) {
        this.form= {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1
      };
      }
    },
    changeonecate() {
      reqcatelist({ pid: this.form.first_cateid }).then(res => {
        this.twocate = res.data.list;
      });
    },
    changetwocate() {
      console.log(111111111111111);
      reqgoodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid
      }).then(res => {
        console.log(res.data.list);

        this.foods = res.data.list;
      });
    },
    // 商品规格数据
    // getattrs() {
    //   let obj = this.specslist.find(item => item.id == this.form.specsid);
    //   this.attrs = obj ? obj.attrs : [];
    //   console.log(this.specslist);
    // },
    add() {
      // this.form.description = this.editor.txt.html();
      // this.form.specsattr = this.form.specsattr.join(",");
      if (this.info.isadd) {
        console.log(this.form.begintime);

        reqseckadd(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqsecklist();
            this.info.isshow = false;
          }
        });
      } else {
        reqseckdit(this.form).then(res => {
          if (res.data.code == 200) {
            this.reqsecklist();
            this.info.isshow = false;
          }
        });
      }
    },
    opened() {
      this.reqspecslist();
      if (!this.info.isadd) {
        this.form = this.seckinfo;
        this.changeonecate();
        this.changetwocate();
        if (!!this.editor) {
          this.editor.txt.html(this.form.description);
        }
      }
    },
    closed() {
      if (!this.info.isadd) {
        this.form= {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1
      };
      }
    }
  },
  mounted() {
    this.reqcatelist();
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>